﻿using System;

namespace FuncAndAction
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1 Func委托测试");
            Console.WriteLine("..1_1写法1");
            //写法1
            Func<int, int> func = TestEasyFunc;
            int[] values = new int[] { 1, 2, 3 };

            Util.PerformFunc(values, TestEasyFunc);
            foreach (var i in values)
            {
                Console.WriteLine("  " + i);
            }
            Console.WriteLine("..1_2写法2");
            //写法2
            Util.PerformFunc(values, new Func<int, int>(v =>
             {
                 return v * 100;

             }));

            foreach (var i in values)
            {
                Console.WriteLine("  " + i);
            }

            Console.WriteLine("2 Action委托测试");
            Console.WriteLine("..2_1写法1");
            Action<int> action = TestEasyAction;
            Util.PerformAction(values, TestEasyAction);
            Console.WriteLine("..2_2写法2");
            Util.PerformAction(values, new Action<int>((v) =>
                 {
                     Console.WriteLine("  " + v);
                 }));

            string[] stringArrays = new string[] { "aa", "bb" };
            Array.ForEach(stringArrays, d =>
              {
                  Console.WriteLine(d);
              });

        }

        static int TestEasyFunc(int value) => value * 100;


        static void TestEasyAction(int value) => Console.WriteLine("  " + value);
    }


    public class Util
    {

        //通过类引入T
        public static void PerformFunc<T>(T[] Values, Func<T, T> func)
        {
            for (int i = 0; i < Values.Length; i++)
            {
                //返回的是T
                Values[i] = func(Values[i]);
            }
        }

        public static void PerformAction<T>(T[] Values, Action<T> action)
        {
            for (int i = 0; i < Values.Length; i++)
            {
                //返回的void
                action(Values[i]);
            }
        }

    }
}
